from research.frame import allk1d
import os
from jili.core import save,load
from jili.calc import forwardwindows
from jili.data.db import getdb_client
from jili.data.db import get_calender_a
import pandas as pd
from tqdm import tqdm
import datetime
#ST股
def deal_is_st(start="2006-04-11",ip="127.0.0.1"):
    db=getdb_client("stock",ip)
    rst={}
    objs_delist={}
    for i in db["stock_info"].find({}):
        obj=i["obj"]
        objs_delist[obj]=i
    for i in db["stock_info_name_change"].find({}):
        if "ST" in i["name"]:
            obj=i["obj"]
            if i["start_date"]>=start:
                start0=i["start_date"]
            else:
                start0=start
            if i["end_date"]:
                end0=i["end_date"]
            elif obj in objs_delist.keys():
                end0=objs_delist[obj]["delist_date"]
            else:
                end0=None
            for d in get_calender_a(start0,end0):
                if d not in rst.keys():
                    rst[d]=[obj]
                else:
                    rst[d].append(obj)
    db.client.close()
    save(rst, url)
    return rst
#创业板股
def deal_is_cyb(start="2006-04-11",ip="192.168.10.44"):
    db=getdb_client("stock",ip)
    rst={}
    objs_delist={}
    for i in db["stock_info"].find({"market" : "创业板"}):
        obj=i["obj"]
        objs_delist[obj]=i
        if i["list_date"] >= start:
            start0 = i["list_date"]
        if i["delist_date"]:
            end0=i["delist_date"]
        else:
            end0 = None
        for d in get_calender_a(start0, end0):
            if d not in rst.keys():
                rst[d] = [obj]
            else:
                rst[d].append(obj)
    db.client.close()
    return rst
#中证500股
def deal_is_zz(start="2006-04-11",ip="192.168.10.44"):
    db=getdb_client("stock",ip)
    rst={}
    objs_delist={}
    for i in db["stock_info"].find({"market" : "创业板"}):
        obj=i["obj"]
        objs_delist[obj]=i
        if i["list_date"] >= start:
            start0 = i["list_date"]
        if i["delist_date"]:
            end0=i["delist_date"]
        else:
            end0 = None
        for d in get_calender_a(start0, end0):
            if d not in rst.keys():
                rst[d] = [obj]
            else:
                rst[d].append(obj)
    db.client.close()
    return rst
#十大流通股东是否有“证券投资基金”十大流通股东是否有“证券投资基金”
def deal_top10_holders_jgrate(ip="127.0.0.1",save_url="top10_holders_jg",share="float_share"):
    db = getdb_client("stock", ip)
    rst = {}
    f = {}
    print("read db")
    size_info = load(r"G:\factor\k1d\new1\size_info")
    for i in db["stock_other_top10_floatholders"].find({}):
        obj= i["obj"]
        date=i["ann_date"]
        end_date=i["end_date"]
        if obj not in f.keys():
            f[obj]={date:{end_date:{i["holder_name"]:i["hold_amount"]}}}
        else:
            t=f[obj]
            if date not in t.keys():
                t[date]={end_date:{i["holder_name"]:i["hold_amount"]}}
            else:
                #if i["holder_name"] not in t[date]:
                if end_date not in t[date].keys():
                    t[date][end_date] = {i["holder_name"]:i["hold_amount"]}
                else:
                    if i["holder_name"] not in t[date][end_date].keys():
                        key=i["holder_name"]
                        t[date][end_date][key]=i["hold_amount"]
    f1={}
    print("calc jgnum")
    for obj,v1 in f.items():
        t={}
        for date,v2 in v1.items():
            end_dates=list(v2.keys())
            if len(end_dates)==1:
                d0 = end_dates[0]
            else:
                d0 = max(end_dates)
            if len(v2)>10:
                print(obj,date,len(v2))
            isjg=0
            for i,vvv in v2[d0].items():
                if "基金" in i:
                    isjg=isjg+vvv
                # elif "保险" in i:
                #     isjg=isjg+vvv
                # elif "信托" in i:
                #     isjg=isjg+vvv
                # elif "资产管理计划" in i:
                #     isjg=isjg+vvv
                # elif "资管计划" in i:
                #     isjg=isjg+vvv
            t[date]=isjg
        f1[obj]=t
    objs_delist = []
    print("by obj")
    for i in db["stock_info"].find({}):
        objs_delist.append(i)
    for j in tqdm(objs_delist):
        obj=j["obj"]
        delist_date=j["delist_date"]
        if obj in f1.keys():
            l=f1[obj]
            ll=list(l.keys())
            ll.sort()
            for i in forwardwindows(ll,2):
                if len(i)==2:
                    start=i[0]
                    end=i[1]
                    t0=l[start]
                    for d in get_calender_a(start, end):
                        float_share=10000*10000*100
                        if d in size_info.keys():
                            if obj in size_info[d].keys():
                                float_share=size_info[d][obj][share]*10000
                        if d not in rst.keys():
                            td={obj:t0/float_share}
                            rst[d]=td
                        else:
                            td=rst[d]
                            td[obj]=t0/float_share
                else:
                    start=i[0]
                    t0 = l[start]
                    if delist_date:
                        for d in get_calender_a(start,delist_date):
                            float_share = 10000 * 10000 * 100
                            if d in size_info.keys():
                                if obj in size_info[d].keys():
                                    float_share = size_info[d][obj][share] * 10000
                            if d not in rst.keys():
                                td = {obj: t0/float_share}
                                rst[d] = td
                            else:
                                td = rst[d]
                                td[obj] = t0/float_share
                    else:
                        for d in get_calender_a(start):
                            float_share = 10000 * 10000 * 100
                            if d in size_info.keys():
                                if obj in size_info[d].keys():
                                    float_share = size_info[d][obj][share] * 10000
                            if d not in rst.keys():
                                td = {obj: t0/float_share}
                                rst[d] = td
                            else:
                                td = rst[d]
                                td[obj] = t0/float_share
    save(rst,save_url)
#十大流通股东是否有“证券投资基金”十大流通股东是否有“证券投资基金”
def deal_top10_floatholders_jgshare(ip="127.0.0.1",save_url="top10_floatholders_jg"):
    db = getdb_client("stock", ip)
    rst = {}
    f = {}
    print("read db")
    size_info = load(r"G:\factor\k1d\new1\size_info")
    for i in db["stock_other_top10_floatholders"].find({}):
        obj= i["obj"]
        date=i["ann_date"]
        end_date=i["end_date"]
        if obj not in f.keys():
            f[obj]={date:{end_date:{i["holder_name"]:i["hold_amount"]}}}
        else:
            t=f[obj]
            if date not in t.keys():
                t[date]={end_date:{i["holder_name"]:i["hold_amount"]}}
            else:
                #if i["holder_name"] not in t[date]:
                if end_date not in t[date].keys():
                    t[date][end_date] = {i["holder_name"]:i["hold_amount"]}
                else:
                    if i["holder_name"] not in t[date][end_date].keys():
                        key=i["holder_name"]
                        t[date][end_date][key]=i["hold_amount"]
    f1={}
    print("calc jgnum")
    for obj,v1 in f.items():
        t={}
        for date,v2 in v1.items():
            end_dates=list(v2.keys())
            if len(end_dates)==1:
                d0 = end_dates[0]
            else:
                d0 = max(end_dates)
            if len(v2)>10:
                print(obj,date,len(v2))
            isjg=0
            for i,vvv in v2[d0].items():
                if "基金" in i:
                    isjg=isjg+vvv
                # elif "保险" in i:
                #     isjg=isjg+vvv
                # elif "信托" in i:
                #     isjg=isjg+vvv
                # elif "资产管理计划" in i:
                #     isjg=isjg+vvv
                # elif "资管计划" in i:
                #     isjg=isjg+vvv
            t[date]=isjg
        f1[obj]=t
    objs_delist = []
    print("by obj")
    for i in db["stock_info"].find({}):
        objs_delist.append(i)
    for j in tqdm(objs_delist):
        obj=j["obj"]
        delist_date=j["delist_date"]
        if obj in f1.keys():
            l=f1[obj]
            ll=list(l.keys())
            ll.sort()
            for i in forwardwindows(ll,2):
                if len(i)==2:
                    start=i[0]
                    end=i[1]
                    t0=l[start]
                    for d in get_calender_a(start, end):
                        if d not in rst.keys():
                            td={obj:t0}
                            rst[d]=td
                        else:
                            td=rst[d]
                            td[obj]=t0
                else:
                    start=i[0]
                    t0 = l[start]
                    if delist_date:
                        for d in get_calender_a(start,delist_date):
                            if d not in rst.keys():
                                td = {obj: t0}
                                rst[d] = td
                            else:
                                td = rst[d]
                                td[obj] = t0
                    else:
                        for d in get_calender_a(start):
                            if d not in rst.keys():
                                td = {obj: t0}
                                rst[d] = td
                            else:
                                td = rst[d]
                                td[obj] = t0
    save(rst,save_url)
#十大流通股东是否有“证券投资基金”十大流通股东是否有“证券投资基金”
def deal_top10_floatholders_jgrate(ip="127.0.0.1",save_url="top10_floatholders_jg",share="float_share"):
    db = getdb_client("stock", ip)
    rst = {}
    f = {}
    print("read db")
    size_info = load(r"G:\factor\k1d\new1\size_info")
    for i in db["stock_other_top10_floatholders"].find({}):
        obj= i["obj"]
        date=i["ann_date"]
        end_date=i["end_date"]
        if obj not in f.keys():
            f[obj]={date:{end_date:{i["holder_name"]:i["hold_amount"]}}}
        else:
            t=f[obj]
            if date not in t.keys():
                t[date]={end_date:{i["holder_name"]:i["hold_amount"]}}
            else:
                #if i["holder_name"] not in t[date]:
                if end_date not in t[date].keys():
                    t[date][end_date] = {i["holder_name"]:i["hold_amount"]}
                else:
                    if i["holder_name"] not in t[date][end_date].keys():
                        key=i["holder_name"]
                        t[date][end_date][key]=i["hold_amount"]
    f1={}
    print("calc jgnum")
    for obj,v1 in f.items():
        t={}
        for date,v2 in v1.items():
            end_dates=list(v2.keys())
            if len(end_dates)==1:
                d0 = end_dates[0]
            else:
                d0 = max(end_dates)
            if len(v2)>10:
                print(obj,date,len(v2))
            isjg=0
            for i,vvv in v2[d0].items():
                if "基金" in i:
                    isjg=isjg+vvv
                # elif "保险" in i:
                #     isjg=isjg+vvv
                # elif "信托" in i:
                #     isjg=isjg+vvv
                # elif "资产管理计划" in i:
                #     isjg=isjg+vvv
                # elif "资管计划" in i:
                #     isjg=isjg+vvv
            t[date]=isjg
        f1[obj]=t
    objs_delist = []
    print("by obj")
    for i in db["stock_info"].find({}):
        objs_delist.append(i)
    for j in tqdm(objs_delist):
        obj=j["obj"]
        delist_date=j["delist_date"]
        if obj in f1.keys():
            l=f1[obj]
            ll=list(l.keys())
            ll.sort()
            for i in forwardwindows(ll,2):
                if len(i)==2:
                    start=i[0]
                    end=i[1]
                    t0=l[start]
                    for d in get_calender_a(start, end):
                        float_share=10000*10000*100
                        if d in size_info.keys():
                            if obj in size_info[d].keys():
                                float_share=size_info[d][obj][share]*10000
                        if d not in rst.keys():
                            td={obj:t0/float_share}
                            rst[d]=td
                        else:
                            td=rst[d]
                            td[obj]=t0/float_share
                else:
                    start=i[0]
                    t0 = l[start]
                    if delist_date:
                        for d in get_calender_a(start,delist_date):
                            float_share = 10000 * 10000 * 100
                            if d in size_info.keys():
                                if obj in size_info[d].keys():
                                    float_share = size_info[d][obj][share] * 10000
                            if d not in rst.keys():
                                td = {obj: t0/float_share}
                                rst[d] = td
                            else:
                                td = rst[d]
                                td[obj] = t0/float_share
                    else:
                        for d in get_calender_a(start):
                            float_share = 10000 * 10000 * 100
                            if d in size_info.keys():
                                if obj in size_info[d].keys():
                                    float_share = size_info[d][obj][share] * 10000
                            if d not in rst.keys():
                                td = {obj: t0/float_share}
                                rst[d] = td
                            else:
                                td = rst[d]
                                td[obj] = t0/float_share
    save(rst,save_url)
#十大流通股东是否有“证券投资基金”十大流通股东是否有“证券投资基金”
def deal_top10_floatholders(ip="127.0.0.1",save_url="top10_floatholders_jg"):
    db = getdb_client("stock", ip)
    rst = {}
    f = {}
    print("read db")
    for i in db["stock_other_top10_floatholders"].find({}):
        obj= i["obj"]
        date=i["ann_date"]
        end_date=i["end_date"]
        if obj not in f.keys():
            f[obj]={date:{end_date:[i["holder_name"]]}}
        else:
            t=f[obj]
            if date not in t.keys():
                t[date]={end_date:[i["holder_name"]]}
            else:
                #if i["holder_name"] not in t[date]:
                if end_date not in t[date].keys():
                    t[date][end_date] = [i["holder_name"]]
                else:
                    if i["holder_name"] not in t[date][end_date]:
                        t[date][end_date].append(i["holder_name"])
    f1={}
    print("calc jgnum")
    for obj,v1 in f.items():
        t={}
        for date,v2 in v1.items():
            end_dates=list(v2.keys())
            if len(end_dates)==1:
                d0 = end_dates[0]
            else:
                d0 = max(end_dates)
            if len(v2)>10:
                print(obj,date,len(v2))
            isjg=0
            for i in v2[d0]:
                if "证券投资基金" in i:
                    isjg=isjg+1
                elif "证券基金" in i:
                    isjg = isjg + 1
                # elif "保险" in i:
                #     isjg=isjg+1
                # elif "信托" in i:
                #     isjg=isjg+1
                # elif "资产管理计划" in i:
                #     isjg=isjg+1
                # elif "资管计划" in i:
                #     isjg=isjg+1
            t[date]=isjg
        f1[obj]=t
    objs_delist = []
    print("by obj")
    for i in db["stock_info"].find({}):
        objs_delist.append(i)
    for j in tqdm(objs_delist):
        obj=j["obj"]
        delist_date=j["delist_date"]
        if obj in f1.keys():
            l=f1[obj]
            ll=list(l.keys())
            ll.sort()
            for i in forwardwindows(ll,2):
                if len(i)==2:
                    start=i[0]
                    end=i[1]
                    t0=l[start]
                    for d in get_calender_a(start, end):
                        if d not in rst.keys():
                            td={obj:t0}
                            rst[d]=td
                        else:
                            td=rst[d]
                            td[obj]=t0
                else:
                    start=i[0]
                    t0 = l[start]
                    if delist_date:
                        for d in get_calender_a(start,delist_date):
                            if d not in rst.keys():
                                td = {obj: t0}
                                rst[d] = td
                            else:
                                td = rst[d]
                                td[obj] = t0
                    else:
                        for d in get_calender_a(start):
                            if d not in rst.keys():
                                td = {obj: t0}
                                rst[d] = td
                            else:
                                td = rst[d]
                                td[obj] = t0
    save(rst,save_url)


#龙虎榜是否有“机构专用”    衍生 N日龙虎榜是否有机构专用stock_daily_top_inst
def deal_top_inst_buy(ip="127.0.0.1",saveurl=r"G:\factor\k1d\new1\top_inst_jg_buy"):
    db = getdb_client("stock", ip)
    objs_delist = []
    rst={}
    cu=db["stock_daily_top_inst"]
    for i in db["stock_info"].find({}):
        obj = i["obj"]
        objs_delist.append(i)
    data0={}
    for i in cu.find({}):
        obj=i["obj"]
        d=datetime.datetime.strptime(i["trade_date"],"%Y%m%d")
        if obj not in data0:
            data0[obj]={}
        if d not in data0[obj].keys():
            data0[obj][d]=[]
        data0[obj][d].append(i)
    data={}
    for obj,v in data0.items():
        for d,vv in v.items():
            keys=[]
            if obj not in data:
                data[obj] = {}
            if d not in data[obj].keys():
                data[obj][d] = 0
            for i in vv:
                key="exalter"+str(i["net_buy"])
                if key not in keys:
                    keys.append(key)
                if i["net_buy"]>0 and ("机构专用" in i["exalter"]):
                    data[obj][d]=data[obj][d]+1
    for i in tqdm(objs_delist):
        obj = i["obj"]
        start = i["list_date"]
        end = i["delist_date"]
        for d in get_calender_a(start, end):
            if d not in data:
                rst[d] = {}
            if obj not in rst[d].keys():
                rst[d][obj] = 0
            if obj in data.keys():
                if d in data[obj].keys():
                    rst[d][obj]=rst[d][obj]
    save(rst,saveurl)
    return rst
def deal_top_inst_sell(ip="127.0.0.1",saveurl=r"G:\factor\k1d\new1\top_inst_jg_sell"):
    db = getdb_client("stock", ip)
    objs_delist = []
    rst={}
    cu=db["stock_daily_top_inst"]
    for i in db["stock_info"].find({}):
        obj = i["obj"]
        objs_delist.append(i)
    data0={}
    for i in cu.find({}):
        obj=i["obj"]
        d=datetime.datetime.strptime(i["trade_date"],"%Y%m%d")
        if obj not in data0:
            data0[obj]={}
        if d not in data0[obj].keys():
            data0[obj][d]=[]
        data0[obj][d].append(i)
    data={}
    for obj,v in data0.items():
        for d,vv in v.items():
            keys=[]
            if obj not in data:
                data[obj] = {}
            if d not in data[obj].keys():
                data[obj][d] = 0
            for i in vv:
                key="exalter"+str(i["net_buy"])
                if key not in keys:
                    keys.append(key)
                if i["net_buy"]<0 and ("机构专用" in i["exalter"]):
                    data[obj][d]=data[obj][d]+1
    for i in tqdm(objs_delist):
        obj = i["obj"]
        start = i["list_date"]
        end = i["delist_date"]
        for d in get_calender_a(start, end):
            if d not in data:
                rst[d] = {}
            if obj not in rst[d].keys():
                rst[d][obj] = 0
            if obj in data.keys():
                if d in data[obj].keys():
                    rst[d][obj]=rst[d][obj]
    save(rst,saveurl)
    return rst

#上市天数
def deal_tarde_days(ip="127.0.0.1",save_url="trade_days"):
    db = getdb_client("stock", ip)
    rst = {}
    objs_delist = []
    t0=datetime.datetime(2000,1,4)
    for i in db["stock_info"].find({}):
        objs_delist.append(i)
    for i in tqdm(objs_delist):
        obj=i["obj"]
        start=i["list_date"]
        end=i["delist_date"]
        if end:
            if datetime.datetime.strptime(end,"%Y%m%d") >t0:
                t1=datetime.datetime.strptime(start,"%Y%m%d")
                if t1 <t0:
                    n=t0-t1
                    n=n.days
                else:
                    n=0
                for d in get_calender_a(start, end):
                    n=n+1
                    if d not in rst.keys():
                        t={obj:n}
                        rst[d] = t
                    else:
                        t=rst[d]
                        t[obj]=n
        else:
            t1 = datetime.datetime.strptime(start, "%Y%m%d")
            if t1 < t0:
                n = t0 - t1
                n = n.days
            else:
                n = 0
            for d in get_calender_a(start, end):
                n = n + 1
                if d not in rst.keys():
                    t = {obj: n}
                    rst[d] = t
                else:
                    t = rst[d]
                    t[obj] = n
    save(rst, save_url)
    return rst
def deal_is_jg(top10_floatholders_jg="top10_floatholders_jg",top_inst_jg="top_inst_jg",name="is_jg",days=60,checkn=1):
    a1=load(os.path.join(r"G:\factor\k1d",top10_floatholders_jg))
    a=load(os.path.join(r"G:\factor\k1d",top_inst_jg))
    rst = {}
    for d, v in tqdm(a1.items()):
        if d <= datetime.datetime(2021, 8, 5):
            f = []
            for obj, v1 in v.items():
                if v1["top10_floatholders_jg"] > 0:
                    if obj not in f:
                        f.append(obj)
            for d1 in get_calender_a(start=d-datetime.timedelta(days=90), end=d):
                for obj, v1 in a[d1].items():
                    if v1["top_inst_jg"] > 0:
                        if obj not in f:
                            f.append(obj)
            rst[d] = f
    return rst
def deal_jg(top10_floatholders_jg="top10_floatholders_jg",top_inst_jg="top_inst_jg",name="jg",days=60):
    a1=load(top10_floatholders_jg)
    a=load(top_inst_jg)
    rst = {}
    for d, v in tqdm(a1.items()):
        if d <= datetime.datetime(2021, 8, 5):
            f = {}
            for obj, v1 in v.items():
                if obj not in f.keys():
                    f[obj]=v1
                else:
                    f[obj] = f[obj] + v1
            for d1 in get_calender_a(start=d-datetime.timedelta(days=days), end=d):
                for obj, v1 in a[d1].items():
                    if obj not in f.keys():
                        f[obj]=v1
                    else:
                        f[obj]=f[obj]+v1
            rst[d] = f
    return rst
def deal_top_inst_N(top_inst_jg="top_inst_jg",save_url="top_inst_jg",days=20):
    '''
    过去N天上过几次龙虎榜机构专用席位
    '''
    a=load(os.path.join(r"G:\factor\k1d",top_inst_jg))
    rst = {}
    save_url = save_url + '_' + str(days)
    dates = list(a.keys())
    for d, v in tqdm(a.items()):
        #if d <= datetime.datetime(2021, 8, 5):
        f = {}
        start = dates[(dates.index(d)) - days + 1]
        for d1 in get_calender_a(start=start, end=d):
            for obj, v1 in a[d1].items():
                if obj not in f.keys():
                    f[obj] = v1
                else:
                    f[obj]=f[obj]+v1
        rst[d] = f
    save(rst, save_url)
    return rst
def calc_lhb_buy(top_inst_jg="top_inst_jg",name="jg_buy_inst",days=60):
    a = load(os.path.join(r"G:\factor\k1d", top_inst_jg))
    rst = {}
    for d in tqdm(a.keys()):
        f = {}
        for d1 in get_calender_a(start=d - datetime.timedelta(days=days), end=d):
            if d1 in a.keys():
                for obj, v1 in a[d1].items():
                    if obj not in f.keys():
                        f[obj] = v1["top_inst_jg"]
                    else:
                        f[obj] = f[obj] + v1["top_inst_jg"]
        rst[d] = f
    return rst


if __name__ == '__main__':
    # a=deal_is_st()
    # save(a, r"G:\factor\k1d\is_st")
    #
    #deal_top10_floatholders_jgrate(save_url=r"G:\factor\k1d\new1\top10_floatholders_jgrate",share="total_share")
    #deal_top10_floatholders_jgshare(save_url=r"G:\factor\k1d\new1\top10_floatholders_jgshare")
    #deal_tarde_days(save_url=r"G:\factor\k1d\new1\trade_days")
    #save(a, r"G:\factor\k1d\new1\trade_days")
    # a = deal_top10_floatholders()
    # save(a, r"G:\factor\k1d\top10_floatholders_jg")

    #deal_top_inst_buy(ip="127.0.0.1", saveurl=r"G:\factor\k1d\new1\top_inst_jg_buy")
    #deal_top_inst_sell(ip="127.0.0.1", saveurl=r"G:\factor\k1d\new1\top_inst_jg_sell")
    #a = deal_tarde_days()
    #save(a, r"G:\factor\k1d\trade_days")
    # a=deal_is_jg()
    # save(a, r"G:\factor\k1d\is_jg90")

    # a = deal_top_inst_sell()
    # save(a, r"G:\factor\k1d\top_inst_jg_sell")
    deal_top10_floatholders(save_url=r"G:\factor\k1d\new1\top10_floatholders_jg")
    # a = deal_jg(top10_floatholders_jg=r"G:\factor\k1d\new1\top10_floatholders_jg", top_inst_jg=r"G:\factor\k1d\new1\top_inst_jg_buy", name="jg", days=60)
    # save(a, r"G:\factor\k1d\new1\jg60")
    # a = deal_jg(top10_floatholders_jg=r"G:\factor\k1d\new1\top10_floatholders_jg", top_inst_jg=r"G:\factor\k1d\new1\top_inst_jg_buy", name="jg", days=40)
    # save(a, r"G:\factor\k1d\new1\jg40")
    # a = deal_jg(top10_floatholders_jg=r"G:\factor\k1d\new1\top10_floatholders_jg", top_inst_jg=r"G:\factor\k1d\new1\top_inst_jg_buy", name="jg", days=20)
    # save(a, r"G:\factor\k1d\new1\jg20")
    # a = deal_jg(top10_floatholders_jg=r"G:\factor\k1d\new1\top10_floatholders_jg", top_inst_jg=r"G:\factor\k1d\new1\top_inst_jg_buy", name="jg", days=10)
    # save(a, r"G:\factor\k1d\new1\jg10")
    # a = calc_lhb_buy(top_inst_jg="top_inst_jg", name="jg_buy_inst", days=60)
    # save(a, r"G:\factor\k1d\jg_buy_inst_60")
    # a = calc_lhb_buy(top_inst_jg="top_inst_jg", name="jg_buy_inst", days=40)
    # save(a, r"G:\factor\k1d\jg_buy_inst_40")
    # a = calc_lhb_buy(top_inst_jg="top_inst_jg", name="jg_buy_inst", days=20)
    # save(a, r"G:\factor\k1d\jg_buy_inst_20")
    # a = calc_lhb_buy(top_inst_jg="top_inst_jg", name="jg_buy_inst", days=10)
    # save(a, r"G:\factor\k1d\jg_buy_inst_10")
    # a = calc_lhb_buy(top_inst_jg="top_inst_jg", name="jg_buy_inst", days=50)
    # save(a, r"G:\factor\k1d\jg_buy_inst_5")
